//
//  PGSeeBigViewController.m
//  youqu
//
//  Created by 彭鹏 on 16/10/22.
//  Copyright © 2016年 pengpeng. All rights reserved.
//

#import "PGSeeBigViewController.h"
#import "UIImageView+WebCache.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import <Photos/Photos.h>
#import "PGPhotoManager.h"
//#import "SVProgressHUD.h"
@interface PGSeeBigViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
/**imageView*/
@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation PGSeeBigViewController
- (IBAction)backButtonClick {
    [self dismissViewControllerAnimated:YES completion:nil];
    
   
    
    
    
    
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
//    if (error) {
//        [SVProgressHUD showErrorWithStatus:@"保存失败"];
//    }else {
//        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
//    }
}



//- (void)saveImage {
//    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
//        //判断相册是否存在
//        PHAssetCollection *assetCollection = [self fetchAssetCollections:@"有趣"];
//        
//        PHAssetCollectionChangeRequest *assetCollectionChangeRequest = nil;
//        if (assetCollection == nil) {
//            //1.创建相册
//            assetCollectionChangeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"有趣"];
//        }else {
//            //存在相册
//            assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
//        }
//        
//        
//        //2.创建图片请求
//        PHAssetCreationRequest *assetCreationRequest = [PHAssetCreationRequest creationRequestForAssetFromImage:_imageView.image];
//        
//        //3.往相册中添加图片
//        [assetCollectionChangeRequest addAssets:@[assetCreationRequest.placeholderForCreatedAsset]];
//        
//        
//    } completionHandler:^(BOOL success, NSError * _Nullable error) {
//        if (success) {
//            [SVProgressHUD showSuccessWithStatus:@"保存成功"];
//        } else {
//            [SVProgressHUD showErrorWithStatus:@"保存失败"];
//        }
//    }];
//}

- (IBAction)saveButtonClick {
    //判断用户是否允许访问app相册
    //获取app访问相册状态
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusAuthorized) {//授权
        //将图片保存到相册
        [PGPhotoManager saveImage:_imageView.image title:@"有趣" completionHandler:^(BOOL success, NSError *error) {
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"保存成功"];
            } else {
                [SVProgressHUD showErrorWithStatus:@"保存失败"];
            }
        }];
    } else if (status == PHAuthorizationStatusNotDetermined) {//不确定
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if (status == PHAuthorizationStatusAuthorized) {
                //将图片保存到相册
                [PGPhotoManager saveImage:_imageView.image title:@"有趣" completionHandler:^(BOOL success, NSError *error) {
                    if (success) {
                        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
                    } else {
                        [SVProgressHUD showErrorWithStatus:@"保存失败"];
                    }
                }];
            }
        }];
    } else {  //拒绝
        [SVProgressHUD showInfoWithStatus:@"请允许有趣访问相册"];
    }
    
    
    
//    UIImageWriteToSavedPhotosAlbum(_imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

//- (void)saveImage:(UIImage *)image {
//    NSLog(@"%@",image);
//}



- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *imageView = [[UIImageView alloc] init];
    
    [_scrollView addSubview:imageView];
    _imageView = imageView;
    _scrollView.delegate = self;
//    NSLog(@"%@",_scrollView.subviews);
    [imageView sd_setImageWithURL:[NSURL URLWithString:_item.image1] placeholderImage:_item.smallImage];
    CGFloat h = SCREENW/_item.width*_item.height;
    _scrollView.minimumZoomScale = 0.5;
    _scrollView.maximumZoomScale = 2;
    if (!_item.is_big) {
        imageView.frame = [UIScreen mainScreen].bounds;
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        
    }else {
        imageView.frame = CGRectMake(0, 0, SCREENW, h);
        _scrollView.contentSize = CGSizeMake(0, h);
       
    }
    
    
    
}

#pragma mark -UIScrollViewDelegate
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return _imageView;
}


@end
